package com.example.firstapp.fragment

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.firstapp.R

class NewsContentActivity : AppCompatActivity() {
    companion object {
        fun actionStart(context: Context, title: String, content: String) {
            val intent = Intent(context, NewsContentActivity::class.java).apply {
                putExtra("news_title", title)
                putExtra("news_content", content)
            }
            context.startActivity(intent)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_news_content)
        println(7)
        val title = intent.getStringExtra("news_title") // 获取传入的新闻标题
        val content = intent.getStringExtra("news_content") // 获取传入的新闻内容
        if (title != null && content != null) {
            println(8)
            // 安全获取Fragment实例
            val fragment =
                supportFragmentManager.findFragmentById(R.id.newsContentFrag) as? NewsContentFragment
            println(fragment)
            // 安全处理数据传递
            intent?.extras?.let {
                val title = it.getString("news_title", "")
                val content = it.getString("news_content", "")
                println(title)
                println(content)
                fragment?.refresh(title, content)
                println(11)
            }
        }
    }
}